/// 网络套接字句柄
pub type Socket = windows_sys::Win32::Networking::WinSock::SOCKET;

///
pub const AF_INET: i32 = windows_sys::Win32::Networking::WinSock::AF_INET as i32;
///
pub const SOCK_STREAM: i32 = windows_sys::Win32::Networking::WinSock::SOCK_STREAM;
///
pub const SOCK_DGRAM: i32 = windows_sys::Win32::Networking::WinSock::SOCK_DGRAM;
/// TCP协议码
pub const IPPROTO_TCP: i32 = windows_sys::Win32::Networking::WinSock::IPPROTO_TCP;
/// UDP协议码
pub const IPPROTO_UDP: i32 = windows_sys::Win32::Networking::WinSock::IPPROTO_UDP;
/// 套接字错误
pub const SOCKET_ERROR: i32 = windows_sys::Win32::Networking::WinSock::SOCKET_ERROR;
/// 表示一个无效的套接字
pub const INVALID_SOCKET: Socket = windows_sys::Win32::Networking::WinSock::INVALID_SOCKET;

/// 创建网络套接字
#[inline(always)]
pub fn socket(domain: i32, ty: i32, protocol: i32) -> Socket
{
    unsafe { windows_sys::Win32::Networking::WinSock::socket(domain, ty, protocol) }
}
/// 关闭网络套接字
#[inline(always)]
pub fn close(socket: Socket) -> i32
{
    unsafe { windows_sys::Win32::Networking::WinSock::closesocket(socket) }
}
/// 绑定端口
#[inline(always)]
pub fn bind(socket: Socket, sockaddr: *const SockAddr) -> i32
{
    unsafe { windows_sys::Win32::Networking::WinSock::bind(socket, sockaddr as *const windows_sys::Win32::Networking::WinSock::SOCKADDR, std::mem::size_of::<SockAddr>() as i32) }
}
/// 开始监听
#[inline(always)]
pub fn listen(socket: Socket, backlog: i32) -> i32
{
    unsafe { windows_sys::Win32::Networking::WinSock::listen(socket, backlog) }
}
/// 接受网络连接
#[inline(always)]
pub fn accept(socket: Socket, sockaddr: *mut SockAddr) -> Socket
{
    unsafe {
        let mut addrlen: i32 = std::mem::size_of::<SockAddr>() as i32;
        windows_sys::Win32::Networking::WinSock::accept(socket, sockaddr as *mut windows_sys::Win32::Networking::WinSock::SOCKADDR, &mut addrlen)
    }
}
/// 连接到网络
#[inline(always)]
pub fn connect(socket: Socket, sockaddr: *const SockAddr) -> i32
{
    unsafe { windows_sys::Win32::Networking::WinSock::connect(socket, sockaddr as *const windows_sys::Win32::Networking::WinSock::SOCKADDR, std::mem::size_of::<SockAddr>() as i32) }
}
/// 接收网络数据
#[inline(always)]
pub fn recv(socket: Socket, buf: *mut u8, len: usize) -> isize
{
    unsafe { windows_sys::Win32::Networking::WinSock::recv(socket, buf, len as i32, 0) as isize }
}
/// 发送网络数据
#[inline(always)]
pub fn send(socket: Socket, buf: *const u8, len: usize) -> isize
{
    unsafe { windows_sys::Win32::Networking::WinSock::send(socket, buf, len as i32, 0) as isize }
}

include!("select.rs");